<!-- 春节不打烊 - 1688 -->
<template>
  <div class="page" ref="pageHeight">
    <div class="goods-content">
      <div class="filter-content">
        <!--常规搜索-->
        <div class="expand-more">
          <div v-if="showMore == false" class="more-search" @click="showMore = true">
            <span class="text">{{ $t('goods.moreFilterConditions') }}</span>
            <img class="arrow" src="@/assets/images/home/down-arrow.png" alt="">
          </div>
          <div v-else class="fun">
            <span class="text" @click="onReset">{{ $t('goods.reset') }}</span>
            <div class="more-search" @click="showMore = false">
              <span class="text">{{ $t('goods.collapse') }}</span>
              <img class="arrow" src="@/assets/images/home/up-arrow.png" alt="">
            </div>
          </div>
        </div>
        <div v-if="showMore == false" class="general-search">
          <div class="left">
            <span class="label" @click="synthesize" :class="{ 'active-left': !form.sort.price }">
              {{ $t('goods.comprehensiveSorting') }}
            </span>
            <t-sort class="sort" @on-change="onSort" v-model="sortPrice" labelActiveColor="#FF186B"></t-sort>
            <!-- <div class="price-search">
              <div class="price-item">
                <Input v-model="form.priceStart" :placeholder="$t('goods.minPrice')" />
              </div>
              <div class="line">-</div>
              <div class="price-item">
                <Input v-model="form.priceEnd" :placeholder="$t('goods.maxPrice')" />
              </div>
              <div class="search">
                <span class="btn" @click="onSearch" long type="primary">{{ $t('goods.search') }}</span>
              </div>
            </div> -->
          </div>
        </div>
        <!--复杂搜索-->
        <div v-else class="complex-search">
          <!--服务质量-->
          <div class="search-item">
            <span class="label">
              {{ $t('goods.serviceQuality') }}
            </span>
            <div class="condition-content">
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.certifiedFactory">
                  {{ $t('goods.certifiedFactory') }}
                </Checkbox>
              </div>
              <div class="item">
                <Dropdown trigger="click" @on-click="onTotalEpScoreLv">
                  <span class="select" :class="formMore.totalEpScoreLv ? 'select-active' : ''">
                    {{ $t('goods.totalEpScore') }}
                    <template v-for="(item, index) in totalEpScoreLvList">
                      <span class="select-text" v-if="item.value == formMore.totalEpScoreLv"
                        :key="item.value + index">{{ item.text }}</span>
                    </template>
                    <Icon type="ios-arrow-down"></Icon>
                  </span>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="item in totalEpScoreLvList" :key="item.value" :name="item.value">
                      {{ item.text }}
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </div>
              <div class="item">
                <Dropdown trigger="click" @on-click="onRate24HLv">
                  <span class="select" :class="formMore.getRate24HLv ? 'select-active' : ''">
                    {{ $t('goods.getRate24H') }}
                    <template v-for="(item, index) in getRate24HList">
                      <span class="select-text" v-if="item.value == formMore.getRate24HLv" :key="item.value + index">{{
                        item.text }}</span>
                    </template>
                    <Icon type="ios-arrow-down"></Icon>
                  </span>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="item in getRate24HList" :key="item.value" :name="item.value">
                      {{ item.text }}
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </div>
              <div class="item">
                <Dropdown trigger="click" @on-click="onRate48HLv">
                  <span class="select" :class="formMore.getRate48HLv ? 'select-active' : ''">
                    {{ $t('goods.getRate48H') }}
                    <template v-for="(item, index) in getRate48HList">
                      <span class="select-text" v-if="item.value == formMore.getRate48HLv" :key="item.value + index">{{
                        item.text }}</span>
                    </template>
                    <Icon type="ios-arrow-down"></Icon>
                  </span>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="item in getRate48HList" :key="item.value" :name="item.value">
                      {{ item.text }}
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </div>
            </div>
          </div>
          <div class="search-item">
            <span class="label">
              {{ $t('goods.serviceGuarantee') }}
            </span>
            <div class="condition-content">
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.shipInToday">
                  {{ $t('goods.shipInToday') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.selection1688">
                  {{ $t('goods.selection1688') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.jxhy">
                  {{ $t('goods.jxhy') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.shipIn24Hours">
                  {{ $t('goods.shipIn24Hours') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.shipIn48Hours">
                  {{ $t('goods.shipIn48Hours') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.noReason7DReturn">
                  {{ $t('goods.noReason7DReturn') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.isOnePsale">
                  {{ $t('goods.isOnePsale') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.isOnePsaleFreePost">
                  {{ $t('goods.isOnePsaleFreePost') }}
                </Checkbox>
              </div>
            </div>
          </div>
          <div class="search-item comprehensive">
            <span class="label">
              {{ $t('goods.comprehensiveSorting') }}
            </span>
            <div class="condition-content">
              <div class="item">
                <t-sort class="sort" @on-change="onSort" v-model="sortPrice" labelSize="0.88542vw" :labelWeight="400"></t-sort>
              </div>
              <div class="price-search">
                <div class="price-item">
                  <Input v-model="form.priceStart" :placeholder="$t('goods.minPrice')" />
                </div>
                <div class="line">-</div>
                <div class="price-item">
                  <Input v-model="form.priceEnd" :placeholder="$t('goods.maxPrice')" />
                </div>
                
              </div>
            </div>
          </div>
					<!-- 关键词 -->
					<div class="search-item">
						<span class="label">
						  {{ $t('goods.key_text') }}
						</span>
						<div class="condition-content">
							<div class="item-keyword">
								<Input v-model="form.keyword" :placeholder="$t('home.searchKeywords')" />
							</div>
							<div class="search-btn">
							  <span class="btns" @click="onSearchBtn" long type="primary">{{ $t('goods.search') }}</span>
							</div>
						</div>
					</div>
        </div>
      </div>
      <Divider />
      <div class="goods-list">
        <product v-for="item in goodsList" :currencySymbol="userCurrency" :info="item" :key="item.spuNo"></product>
      </div>

      <div class="loading" v-if="goodsList.length == 0 && loading">
        <t-loading></t-loading>
      </div>
      <t-no-data v-if="goodsList.length == 0 && !loading"></t-no-data>
    </div>
    <div class="more" v-if="!noData">
      <Button v-if="goodsList.length > 0" class="btn" :loading="loading" @click="onLoadMore">{{ $t('goods.viewMore')
        }}</Button>
    </div>
    <!-- 禁搜词黑名单 -->
    <tips-text ref="tipsPopRef"></tips-text>
    <!-- 禁搜词灰名单 -->
    <gray-list ref="grayPopRef"></gray-list>
  </div>
</template>

<script>
import Product from "./components/product";
import { productImageQuery, productKeywordQuery, productUploadImg } from "@/api/product";
import TLoading from "@/components/t-loading";
import TSort from "@/components/t-sort";
import TNoData from "@/components/t-no-data";
import { mapState } from "vuex";
import tipsText from "./components/tips-text.vue";
import GrayList from "./components/gray-list.vue";
export default {
  name: "alibaba-goods",
  components: { TNoData, TSort, TLoading, Product, tipsText, GrayList },
  props: {
    loadData: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      getRate48HList: [
        {
          text: '>=99%',
          value: 'getRate48HLv1'
        },
        {
          text: '>=95%',
          value: 'getRate48HLv2'
        },
        {
          text: '<95%',
          value: 'getRate48HLv3'
        }
      ], // 24小时揽收率
      getRate24HList: [
        {
          text: '>=99%',
          value: 'getRate24HLv1'
        },
        {
          text: '>=95%',
          value: 'getRate24HLv2'
        },
        {
          text: '<95%',
          value: 'getRate24HLv3'
        }
      ], // 24小时揽收率
      totalEpScoreLvList: [
        {
          text: this.$t('goods.stars'),
          value: 'totalEpScoreLv1'
        },
        {
          text: this.$t('goods.starsTo50Stars'),
          value: 'totalEpScoreLv2'
        },
        {
          text: this.$t('goods.starsTo45Stars'),
          value: 'totalEpScoreLv3'
        },
        {
          text: this.$t('goods.below4Stars'),
          value: 'totalEpScoreLv4'
        }
      ], // 综合体验分 列表
      showMore: true,
      loading: false, // 加载
      goodsList: [], // 商品列表
      totalPage: 1, // 总页数
      sortPrice: '', // 排序
      noData: false, // 是否还有更多数据
      formMore: {
        totalEpScoreLv: '', // 综合体验分
        getRate24HLv: '', // 24小时揽收率
        getRate48HLv: '', // 48小时揽收率
        certifiedFactory: false,// 认证工厂 certifiedFactory
        shipInToday: false, // 当日发货
        selection1688: false, // 严选
        jxhy: false, // 精选货源
        shipIn24Hours: false, // 24小时发货
        shipIn48Hours: false, // 48小时发货
        noReason7DReturn: false, // 7天无理由
        isOnePsale: false, // 支持一件代发
        isOnePsaleFreePost: false, // 支持包邮
      }, // 更多搜索条件
      form: {
        channel: '1688',
        imageId: '',
        keyword: '', // 关键词，必传
        categoryId: '', // 类目id
        filter: '', // 筛选参数，多个通过英文逗号分隔，枚举参见解决方案介绍 shipInToday,ksCiphertext
        outMemberId: '', // 外部用户id
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 30, // pageSize,示例值(20)
        priceEnd: '', // 批发价结束
        priceStart: '', // 批发价开始
        sort: {
          price: ''
        },// 排序参数，枚举参见解决方案介绍,{"price":"asc"}
				productCollectionId:'261992432'
      },
			copyKey:'',//为了加载更多做对比
    }
  },
  computed: {
    ...mapState({
      userCurrency: (state) => {
        return state.user.userCurrency.code;
      },
      config: (state) => state.config.config,
      searchAgreement: (state) => {
        // 搜索协议
        let obj = '';
        if (state.config.config) {
          obj = state.config.config.articleMap.CFG_ARTICLE_SEARCH;
        }
        return obj;
      }
    })
  },
  watch: {
    '$route'() {
      this.onSearchGoods();
    },
    loadData() {
      this.onSearchGoods();
    }
  },
  created() {
    this.searchInit();
  },
  methods: {
		// 更多搜索
    moreSearches() {
      let filterText = '';
      if (this.formMore.totalEpScoreLv !== '') {
        filterText += this.formMore.totalEpScoreLv + ',';
      }
      if (this.formMore.getRate24HLv !== '') {
        filterText += this.formMore.getRate24HLv + ',';
      }
      if (this.formMore.getRate48HLv !== '') {
        filterText += this.formMore.getRate48HLv + ',';
      }
      if (this.formMore.certifiedFactory) {
        filterText += 'certifiedFactory,';
      }
      if (this.formMore.shipInToday) {
        filterText += 'shipInToday,';
      }
      if (this.formMore.selection1688) {
        filterText += '1688Selection,';
      }
      if (this.formMore.jxhy) {
        filterText += 'jxhy,';
      }
      if (this.formMore.shipIn24Hours) {
        filterText += 'shipIn24Hours,';
      }
      if (this.formMore.shipIn48Hours) {
        filterText += 'shipIn48Hours,';
      }
      if (this.formMore.noReason7DReturn) {
        filterText += 'noReason7DReturn,';
      }
      if (this.formMore.isOnePsale) {
        filterText += 'isOnePsale,';
      }
      if (this.formMore.isOnePsaleFreePost) {
        filterText += 'isOnePsaleFreePost,';
      }
      this.form.filter = filterText;
			if(this.form.keyword == ''){
				this.$Message.error(this.$t('home.searchKeywords'));
				return;
			}
      this.onDefault();
      this.search();
    },
		// 重置
    onReset() {
			if(this.form.keyword == ''){
				this.$Message.error(this.$t('home.searchKeywords'));
				return;
			}
      this.formMore = {
        totalEpScoreLv: '', // 综合体验分
        getRate24HLv: '', // 24小时揽收率
        getRate48HLv: '', // 48小时揽收率
        certifiedFactory: false,// 认证工厂 certifiedFactory
        shipInToday: false, // 当日发货
        selection1688: false, // 严选
        jxhy: false, // 精选货源
        shipIn24Hours: false, // 24小时发货
        shipIn48Hours: false, // 48小时发货
        noReason7DReturn: false, // 7天无理由
        isOnePsale: false, // 支持一件代发
        isOnePsaleFreePost: false, // 支持包邮
      };
			this.onSearchGoods();
    },
		// 48小时揽收率
    onRate48HLv(name) {
      this.formMore.getRate48HLv = name;
      this.moreSearches();
    },
		// 24小时揽收率
    onRate24HLv(name) {
      this.formMore.getRate24HLv = name;
      this.moreSearches();
    },
		// 综合体验分
    onTotalEpScoreLv(name) {
      this.formMore.totalEpScoreLv = name;
      this.moreSearches();
    },
    onSearchGoods() {
      if (!this.loadData) {
        return false;
      }
      this.goodsList = [];
      this.form = {
				...this.form,
        channel: '1688',
        imageId: '',
        categoryId: '', // 类目id
        filter: '', // 筛选参数，多个通过英文逗号分隔，枚举参见解决方案介绍 shipInToday,ksCiphertext
        outMemberId: '', // 外部用户id
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 30, // pageSize,示例值(20)
        priceEnd: '', // 批发价结束
        priceStart: '', // 批发价开始
        sort: {
          price: ''
        },// 排序参数，枚举参见解决方案介绍,{"price":"asc"}
				productCollectionId:'261992432'
      };
      this.noData = false;
      this.search();
    },
		// 重置默认数据
    onDefault() {
      this.goodsList = [];
      this.form.pageNo = 1;
      this.noData = false;
    },
		//展开更多 - 搜索
    onSearchBtn() {
			if(this.form.keyword == ''){
				this.$Message.error(this.$t('home.searchKeywords'));
				return;
			}
      // 搜索商品
      this.onDefault();
      this.getProductKeywordQuery();
    },
    //综合排序
    synthesize() {
      this.sortPrice = '';
      this.form.sort.price = '';
			if(this.form.keyword == ''){
				this.$Message.error(this.$t('home.searchKeywords'));
				return;
			}
      this.onDefault();
      this.search();
    },
		//价格排序
    onSort(name) {
      this.form.sort.price = name;
			if(this.form.keyword == ''){
				this.$Message.error(this.$t('home.searchKeywords'));
				return;
			}
      this.onDefault();
      this.search();
    },
    search() {
      this.getProductKeywordQuery();
    },
		//初始化
		searchInit(){
			let text = this.$route.query.text || this.$route.params.text;
			if (text) {
			  this.form.keyword = text;
			}
			this.search();
		},
		//加载更多
    onLoadMore() {
			if(this.form.keyword == ''){
				this.$Message.error(this.$t('home.searchKeywords'));
				return;
			}
			if(this.copyKey == this.form.keyword){
				this.form.pageNo++;
			}else{
				//当关键词与上一次不一致时，加载更多变成重新请求第一页的内容
				this.copyKey = this.form.keyword
				this.onDefault();
			}
      this.getProductKeywordQuery();
    },
		// 搜索文字商品
    getProductKeywordQuery() {
      if (this.noData == true) {
        return false;
      }
      this.loading = true;
      productKeywordQuery(this.form).then((res) => {
				this.copyKey = this.form.keyword;
        // 禁搜词黑名单
        if (res.code == 1010) {
          this.loading = false;
          this.$refs.tipsPopRef.open(res.message);
        } else if (res.code == 200) {
          // 禁搜词灰度名单弹框
          if (res.message) {
            this.$refs.grayPopRef.open(res.message);
          }
          let data = res.result.records;
					this.goodsList = this.form.pageNo == 1 ? data : this.goodsList.concat(data)
          this.noData = res.result.records.length == 0;
          this.totalPage = res.result.pages;
          this.loading = false;
        } 
      }).catch(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/.ivu-checkbox-checked .ivu-checkbox-inner {
  background-color: #FF186B;
  border-color: #FF186B;
}

/deep/.ivu-input {
  border: none;
  background: #F7F8FB;
  border-radius: 27px;
  padding: 0 31px;
  color: #74767F;
}

/deep/.ivu-input:hover {
  border-color: #FF186B;
}

/deep/.ivu-input:focus {
  border-color: #FF186B;
  box-shadow: none;
}

/deep/.ivu-btn:active,
.ivu-btn.active {
  color: #FF186B;
  border-color: #FF186B;
}

.page {
  height: 100%;

  .scroll-content {
    height: 100%;
  }

  .tabs-wrap {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid @t-border-color;

    .tabs {
      display: flex;
      align-items: center;

      .tab-item {
        display: flex;
        align-items: center;
        padding: 0px 16px;
        opacity: 1;
        color: @t-title-color;
        font-size: 16px;
        height: 46px;
        border-bottom: 2px solid rgba(0, 0, 0, 0);
        margin: 0 60px;
        cursor: pointer;

        &:hover {
          color: @primary-color;
        }
      }

      .tab-active {
        color: @primary-color;
        border-bottom: 2px solid @primary-color;
      }
    }

    .agreement {
      display: flex;
      align-items: center;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      font-size: 14px;

      .agreement-text {
        color: @t-title-color;
      }

      .agreement-value {
        cursor: pointer;
        color: @primary-color;
        font-weight: 500;
      }
    }
  }

  .filter-content {
    margin-bottom: 40px;
    position: relative;

    .expand-more {
      position: absolute;
      right: 0;
      user-select: none;

      .more-search {
        display: flex;
        align-items: center;
        cursor: pointer;
        color: @t-text-color;
        font-size: 14px;

        .text {
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 19px;
          color: #333333;
          line-height: 27px;
        }

        .arrow {
          display: block;
          width: 16px;
          margin-left: 6px;
        }
      }

      .fun {
        display: flex;
        align-items: center;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 19px;
        color: #333333;
        line-height: 27px;

        .text {
          cursor: pointer;
        }

        .more-search {
          display: flex;
          align-items: center;
          margin-left: 24px;
          cursor: pointer;

          .arrow {
            display: block;
            width: 16px;
            margin-left: 6px;
          }
        }
      }
    }

    .general-search {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .left {
        display: flex;
        align-items: center;

        .label {
          min-width: 186px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 24px;
          color: #333333;
          line-height: 27px;
          margin-right: 22px;
          cursor: pointer;
        }

        .active-left {
          color: rgb(255, 24, 107);
        }

        .sort {
          margin-right: 28px;

          &:first-child {
            margin-right: 48px;
          }
        }

        .price-search {
          display: flex;
          align-items: center;

          .price-item {
            width: 140px;
          }

          .line {
            margin: 0 14px;
            color: @t-text-color;
            font-size: 14px;
          }

          
        }
      }
    }

    .complex-search {
      color: @t-text-color;
      font-size: 14px;

      .search-item {
        display: flex;
        justify-content: space-between;
        margin-top: 24px;
				.item-keyword{
					width:312px;
					border-radius: 40px;
					overflow:hidden;
				}
				.search-btn {
				  margin-left: 24px;
					cursor: pointer;
				  .btns {
				    font-family: PingFang SC, PingFang SC;
				    font-weight: 400;
				    font-size: 17px;
				    color: #FF186B;
				    line-height: 27px;
				    padding: 6px 23px;
				    border-radius: 27px;
				    border: 1px solid #FF186B;
				  }
				}
        &:first-child {
          margin-top: 0;
        }

        .label {
          min-width: 186px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 19px;
          color: #767B93;
          line-height: 27px;
          margin-right: 22px;
        }

        .condition-content {
          flex: 1;
          display: flex;
          align-items: center;
          flex-wrap: wrap;
          font-size: 17px;
          color: #74767F;
          line-height: 27px;

          .item {
            min-width: 100px;
            margin-right: 20px;

            .ivu-checkbox-wrapper {
              font-size: 17px;
            }

            .select {
              cursor: pointer;
              display: flex;
              align-items: center;

              .select-text {
                margin: 0 6px;
              }
            }

            .select-active {
              color: @primary-color
            }
          }

          .price-search {
            display: flex;
            align-items: center;

            .price-item {
              width: 140px;
            }

            .line {
              margin: 0 14px;
              color: @t-text-color;
              font-size: 14px;
            }

            .search {
              margin-left: 24px;

              .btn {
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                font-size: 17px;
                color: #FF186B;
                line-height: 27px;
                padding: 6px 23px;
                border-radius: 27px;
                border: 1px solid #FF186B;
              }
            }
          }
        }
      }

      .comprehensive {
        align-items: center;
      }
    }
  }

  .goods-content {
    margin-top: 40px;

    .search-value {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
      color: @t-text-color;
      font-size: 14px;

      span {
        color: @primary-color;
      }

      .search-image {
        display: block;
        width: 80px;
      }
    }

    .goods-list {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 22px 12px;
    }
  }

  .more {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 32px;
    margin-bottom: 32px;

    .btn {
      width: 20%;
      font-size: 16px;
      height: 40px;
    }
  }
}
</style>